﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPatternsAndObjectManagement.Singletons
{
    public class Singleton_DoubleLocked
    {
        //用来做锁的，避免并发时数据被
        //static 保证了该对象不会变只会在第一次初始化Singleton_DoubleLocked对象时，被初始化。
        private static readonly object _lock = new object();
        //提供给外面使用的实体。
        private static Singleton_DoubleLocked? _instance;
        private Singleton_DoubleLocked() { }

        public static Singleton_DoubleLocked Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                        {
                            _instance = new Singleton_DoubleLocked();
                        }
                    }
                }
                return _instance;
            }
        }
    }
}
